package com.wangyonghao.harmonyocrs.utils;

import com.wangyonghao.harmonyocrs.pojo.Result;
import okhttp3.*;

import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class HttpUtils {
    //    private static final String preFix = "http://43.142.11.226:8080/ocrs-server-0.0.1-SN";
    private static final String preFix = "http://192.168.43.77:9090";

    private static String Authorization = "BearereyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE2NTM2OTEzNzU4NDksImV4cCI6MTY1NDI5NjE3NX0.rSayZDqse0VCBaETUK32vf8rl837vT3BWX_TiEYc6QcYT5GT3efQEkVL-fmFUUtxQPklX1JDcGJY6u8qhsULLA";
    private static final OkHttpClient client = new OkHttpClient();
    private static final ExecutorService executorService = Executors.newFixedThreadPool(3);
    public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");

    public String postToFormBody(String path, Map<String, Object> map) {
        String resStr = null;
        try {
            Future<String> r1 = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    FormBody.Builder builder = new FormBody.Builder();
                    for (String key : map.keySet()) {
                        builder.add(key, String.valueOf(map.get(key)));
                    }
                    FormBody body = builder.build();
                    Request request = new Request.Builder()
                            .url(preFix + path)
                            .post(body)
                            .header("Authorization", Authorization)
                            .build();
                    // 创建Call对象，并调用它的execute()方法发送请求并获取服务器返回的数据
                    Response response = client.newCall(request).execute();
                    return response.body().string();
                }
            });
            resStr = r1.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resStr;
    }

    public String postToRequestBody(String path, String json) {
        String resStr = null;
        try {
            Future<String> r1 = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    RequestBody body = RequestBody.create(JSON, json);
                    Request request = new Request.Builder()
                            .url(preFix + path)
                            .post(body)
                            .header("Authorization", Authorization)
                            .build();
                    Response response = client.newCall(request).execute();
                    return response.body().string();
                }
            });
            resStr = r1.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resStr;
    }


    public static String getAuthorization() {
        return Authorization;
    }

    public static void setAuthorization(String authorization) {
        Authorization = authorization;
    }

    public String postToFormURL(String path, Map<String, Object> map) {
        String resStr = null;
        try {
            Future<String> r1 = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    FormBody.Builder builder = new FormBody.Builder();
                    String url = preFix + path;
                    if (map!=null && map.size() > 0) {
                        url += "/?";
                        for (String key : map.keySet()) {
                            url = url + key + "=" + String.valueOf(map.get(key) + "&");
                        }
                        url.substring(0,url.length() - 1);
                    }
                    RequestBody body = builder.build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(body)
                            .header("Authorization", Authorization)
                            .build();
                    // 创建Call对象，并调用它的execute()方法发送请求并获取服务器返回的数据
                    Response response = client.newCall(request).execute();
                    return response.body().string();
                }
            });
            resStr = r1.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resStr;
    }

    public String deleteToFormURL(String path, Map<String, Object> map) {
        String resStr = null;
        try {
            Future<String> r1 = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    FormBody.Builder builder = new FormBody.Builder();
                    String url = preFix + path;
                    if (map!=null && map.size() > 0) {
                        url += "/?";
                        for (String key : map.keySet()) {
                            url = url + key + "=" + String.valueOf(map.get(key) + "&");
                        }
                        url.substring(0,url.length() - 1);
                    }
                    FormBody body = builder.build();
                    Request request = new Request.Builder()
                            .url(url)
                            .delete(body)
                            .header("Authorization", Authorization)
                            .build();
                    // 创建Call对象，并调用它的execute()方法发送请求并获取服务器返回的数据
                    Response response = client.newCall(request).execute();
                    return response.body().string();
                }
            });
            resStr = r1.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resStr;
    }
}
